<template>
    <div class="mainDiv">
        <div class="rowHead">
            <el-button type="success" icon="plus" @click="add" v-qx="permission.addone">新增职位</el-button>
        </div>
        <el-collapse v-model="panposi">
            <el-collapse-item :name="c.sid" v-for="c in minorlist" :key="c.sid">
                <template slot="title">
                        <span style="margin-left: 20px">{{c.name}}</span>
                        <el-button-group style="float:right;margin-right:10px;margin-top: 9px;">
                            <el-button size="small" type="success" icon="plus" @click="add($event,c.id)" v-qx="permission.edittwo">职称</el-button>
                            <el-button size="small" type="warning" icon="edit" @click="edit($event,c)" v-qx="permission.editone">编辑</el-button>
                            <el-button size="small" type="danger" icon="delete2" @click="del($event,c.id)" v-qx="permission.delone">删除</el-button>
                        </el-button-group>
                    

                </template>
                <div class="block">
                    <div class="block width ov ptb6 border-b" v-for="o in optslist" v-if="o.positionid===c.id" :key="o.positionid">
                        <span style="margin-left: 40px">{{o.name}}</span>
                        <div style="float:right;margin-right:10px;">
                            <el-button type="warning" icon="edit" size="mini" @click="edit($event,o)" v-qx="permission.edittwo">编辑</el-button>
                            <el-button type="danger" icon="delete2" size="mini" @click="del($event,o.id)" v-qx="permission.deltwo">删除</el-button>
                            <el-button class="fr mr" type="info" icon="h-jiesuo" size="mini" @click="jichu(o.id,'zhicheng')" v-qx="permission.basis">基础权限</el-button>
                            <el-button class="fr mr" type="info" icon="h-suo" size="mini" v-show="c.showmd===1 && o.showmd===1" @click="mendian(o.id,'zhicheng')" v-qx="permission.mendian">门店权限</el-button>
                        </div>
                    </div>
                </div>
            </el-collapse-item>
        </el-collapse>
    </div>
</template>
<script>
import C from '@/common/common.js'
export default {
  name: 'positionList',
  data () {
    return {
      minorlist: [],
      optslist: [],
      tabposi: '1',
      panposi: [],
      permission: {
        addone: 'zhiwei_addone',
        editone: 'zhiwei_editone',
        delone: 'zhiwei_delone',
        addtwo: 'zhiwei_addtwo',
        edittwo: 'zhiwei_edittwo',
        deltwo: 'zhiwei_deltwo',
        basis: 'zhiwei_basis',
        mendian: 'zhiwei_mendian'
      }
    }
  },
  mounted () {
    this.reupdate()
  },
  methods: {
    async add (eve, posi) {
      eve.stopPropagation()
      let res = await C.openBrowserWindow('/positionAdd', { po: posi }, {
        transparent: true,
        frame: false,
        width: 420,
        height: 280,
        useSpa: false,
        spaModel: 'organization'
      })
      if (res) {
        C.success(res.data)
        this.reupdate()
      }
    },
    async edit (eve, obj) {
      eve.stopPropagation()
      let res = await C.openBrowserWindow('/positionAdd', { obj: obj }, {
        transparent: true,
        frame: false,
        width: 420,
        height: 280,
        useSpa: false,
        spaModel: 'organization'
      })
      if (res) {
        C.success(res.data)
        this.reupdate()
      }
    },
    del (eve, id) {
      eve.stopPropagation()
      let that = this
      C.confirm('确定删除当前职位或职称？', async function () {
        let res = await C.ajax('/organization/zhiwei/del', { id: id }, 'post')
        if (res) {
          C.success(res.data)
          that.reupdate()
        }
      })
    },
    async mendian (id, t) {
      let data = await C.openBrowserWindow('/ruleFloorpermission', { id: id, grouptype: t }, {
        transparent: true,
        frame: false,
        width: 860,
        height: 800,
        useSpa: false,
        spaModel: 'organization'
      })
      if (data) C.success(data.data.info)
    },
    async jichu (id, type) {
      let data = await C.openBrowserWindow('/editRule', { id: id, grouptype: type }, {
        transparent: true,
        frame: false,
        width: 1000,
        height: 800,
        useSpa: false,
        spaModel: 'organization'
      })
      if (data) C.success(data.info)
    },
    async reupdate () {
      let that = this
      let data = await C.ajax('/organization/zhiwei/zhiweilist', {}, 'post')
      if (data) {
        let mino = data.minor
        let opts = data.opts
        for (let i = 0; i < mino.length; i++) {
          mino[i].sid = mino[i].id.toString()
        }
        for (let j = 0; j < opts.length; j++) {
          opts[j].sid = opts[j].id.toString()
        }
        that.minorlist = mino
        that.optslist = opts
      }
    }
  }
}
</script>
<style>
.mt8 {
    margin-top: 8px;
}

.mr8 {
    margin-right: 8px;
}

.ptb6 {
    padding: 6px 0;
}
</style>